#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: comments.py
@contact: zwb15083976291@163.com
@time: 2022/03/10
"""
from rest_framework import serializers

from common.models import BlogComments
from utils.serializers.serializer import UpdateBaseSerializeMethodMixin, SpecifySerializeFieldModelSerializer, \
    PaginatorSerializer


class CommentsSerializers(UpdateBaseSerializeMethodMixin, SpecifySerializeFieldModelSerializer):
    id = serializers.IntegerField()
    comments_type = serializers.ChoiceField(choices=[0, 1])

    class Meta:
        model = BlogComments
        fields = '__all__'


# 删除评论
class DelCommentsSerializers(CommentsSerializers):
    id = serializers.IntegerField()

    def validate_id(self, value):
        if not BlogComments.objects.filter(id=value).exists():
            raise serializers.ValidationError("id不存在")
        return value


# 删除多条评论
class DelListCommentsSerializers(CommentsSerializers):
    ids = serializers.ListField()

    def validate_ids(self, values):
        if not BlogComments.objects.filter(id__in=values).count() == len(values):
            raise serializers.ValidationError("id不存在")
        return values


# 评论回复
class PostCommentsSerializers(CommentsSerializers):
    id = serializers.IntegerField()
    content = serializers.CharField(min_length=1)

    def validate_id(self, value):
        if not BlogComments.objects.filter(id=value).exists():
            raise serializers.ValidationError("评论不存在")
        return value






class CommentsPaginatorSerializer(PaginatorSerializer):
    comments_type = serializers.ChoiceField(choices=[0, 1])
